<?php

/**
 * Controller_num
 * 班主任管理-学号分配
 * 
 */

class Controller_num extends MyApp {

	public function __construct()
	{
		parent::__construct();

		$this->model->staff  	 = YOU::loadModel('staff');
		$this->model->class   	 = YOU::loadModel('class');
		$this->model->student    = YOU::loadModel('student');
		$this->model->divide     = YOU::loadModel('divide');
		$this->model->log        = YOU::loadModel('log');

	}

	/** 首页 **/
	public function actionIndex()
	{

	}

	public function actionlist()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$user=$this->user['type_id'];

		// 老师有哪些班级
		$appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint` WHERE staff_id='$user' AND appoint_type = '4'");
		if (!empty($appoint_lists)) {
			// 获得班级的名称
			foreach ($appoint_lists as $key => $value){
				// 班级名称
				$class_data = $this->model->class->getinfo($value['class_id']);
				$appoint_lists[$key]['class_name'] = $class_data['class_name'];
			}
			// 获得班级的id
			$class_id   = (int)$this->get->class_id;
			if(empty($class_id)){
				foreach ($appoint_lists as $key => $value){
					$class_id = $appoint_lists[0]['class_id'];
				}
			}
			// 查找班级中所有的学生
			$student_lists = $this->model->student->getinfoclass($class_id);
			foreach ($student_lists as $key => $value){

				// 班级名称
				$class_data = $this->model->class->getinfo($value['class_id']);
				$student_lists[$key]['class_name'] = $class_data['class_name'];

			}

			if ($_POST){

				foreach ($student_lists as $key => $value)
				{
					$student_num     = $_POST["student_num_{$value['student_id']}"];
					$student_num 	 = $student_num[0];
					// 写入数据库
					$info = array(

					"student_seat"      	 => $student_num,
					"status"             => 1,
					"create_time"        => date("Y-m-d H:i:s"),
					"update_time"        => "0000-00-00 00:00:00",

					);
					$this->db->do_update("tbl_student", $info, "student_id='{$value['student_id']}'");
				}
				// 跳转
				Helper_Client::location("安排座号成功", "index.php?a=num.list&class_id={$class_id}");
			}
		}


		$this->tpl->assign("student_lists", $student_lists);
		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->display("num/num.list.tpl.php");

	}

}

?>